package com.cstam.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.cstam.dao.UserDao;
import com.cstam.domain.User;

@Service//("userManager")

public class UserManagerImpl implements UserManager{
	
	@Autowired
	//@Qualifier("userDao")
	private UserDao userDao;

	public List<User> getAllUsers() {		
		return userDao.getUsers();

	}

	public User getUserByIduser(int iduser) {		
		return userDao.getUserByIduser(iduser);
	}
	
	@Override
	public User getUserByUsername(String username) {
		return userDao.getUserByUsername(username);
	}

	public int creatUser(String username, String password, int type) {
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		user.setType(2);
		
		return userDao.createUser(user);
	}

	@Override
	public int register(String username, String pwd) {
		int ret=creatUser(username, pwd, 2);
		if(ret==0)
			return -1;
		else{
			User user=userDao.getUserByUsername(username);
			return user.getIduser();
		}
	}

	@Override
	public boolean permissionGrant(int userId) {
		// TODO Auto-generated method stub
		int ret=userDao.grantPermission(1,userId);
		if(ret>0)
			return true;
		else
			return false;
	}

	@Override
	public int login(String username, String pwd) {
		// TODO Auto-generated method stub
		return userDao.login(username, pwd);
	}

	@Override
	public List<User> getUser() {
		// TODO Auto-generated method stub
		return userDao.getUsers();
	}

	@Override
	public boolean changePwd(int userid,String oldpwd, String newpwd) {
		// TODO Auto-generated method stub
		return false;
	}


}
